package com.klhappy.houyuan.activity;

import android.os.Bundle;
import android.util.Log;
import android.widget.GridView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.klhappy.houyuan.adapter.GoodsClassAdapter;
import com.klhappy.houyuan.bean.AllGoodsRecord;
import com.klhappy.houyuan.bean.BaseJsonBean;
import com.klhappy.houyuan.constant.UrlConstant;
import com.klhappy.houyuan.interfaces.CallBackInterModle;
import com.klhappy.houyuan.modle.HouYuanModle;
import com.klhappy.houyuan.utils.JsonCode;
import com.klhappy.houyuan.view.AbsActivity;
import com.klhappy.houyuan.R;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

/**
 * Created by MrPu on 15/11/25.
 */
public class GoodClassActivity extends AbsActivity implements CallBackInterModle {

    @InjectView(R.id.title)
    TextView title;

    @InjectView(R.id.class_grid)
    GridView classgrid;

    @OnClick(R.id.click_ra)
    public void click_ra(){
        finish();
    }

    private int goodsId;
    private String name;
    private HouYuanModle modle;
    private AllGoodsRecord record;
    private List<AllGoodsRecord.Record> list;
    private GoodsClassAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.goods_class);
        ButterKnife.inject(this);

        goodsId = getIntent().getIntExtra("id", 0);
        name = getIntent().getStringExtra("name");
        title.setText(name);
        modle = new HouYuanModle(this);
        modle.setCallBackInterModle(this);

        modle.getGoodsIn(goodsId);
        showDialog("请稍等...");
    }

    @Override
    public void CallBackFromModle(String url, BaseJsonBean o) {
        if (url.equals(UrlConstant.GOODS_URL)){
            closeDialog();
            if (o.getCode() == 200){
                record = JsonCode.getClass(o.getEntity() ,AllGoodsRecord.class);
                list = JsonCode.getClassList(record.getRecords(), AllGoodsRecord.Record.class);
                adapter = new GoodsClassAdapter(list ,this);
                classgrid.setAdapter(adapter);
            }
        }
    }
}
